package com.itheima.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter("/*")
//1.创建一个类实现Filter接口
//2.重写里面最关键的doFilter方法。
//3.实现过滤之后不要忘记filterChain.doFilter()方法放行，如果没有这个方法，就是拦路抢劫！不会放行！
public class EncodingFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //实现中文乱码问题
       request.setCharacterEncoding("utf-8");
       response.setContentType("text/html;charset=utf-8");
        //放行
        filterChain.doFilter(request,response);
    }
}
